#include "Dictionary/Dictionary.h"
#include <sstream>
using std::istringstream;

Dictionary *Dictionary::_pInstance = nullptr;
Dictionary::AutoRelease Dictionary::_ar;

Dictionary *Dictionary::getInstance()
{
    if (nullptr == _pInstance)
    {
        _pInstance = new Dictionary();
    }
    return _pInstance;
}
// void Dictionary::destroy()
// {
//     if (_pInstance)
//     {
//         delete _pInstance;
//         _pInstance = nullptr;
//     }
// }

Dictionary::Dictionary()
{
    ifstream ifs("data/dict.dat");
    if (!ifs.is_open())
    {
        LogError("open dict.dat failed");
    }
    string line; // 获取每一行
    string word;
    int num;
    pair<string, int> pr;
    while (getline(ifs, line))
    {
        // 将标点符号换成空格
        for (auto &ch : line)
        {
            if (ch == ':')
            {
                ch = ' ';
            }
        }
        // 组成一个pair<string，int>
        istringstream iss(line);
        iss >> word;
        iss >> num;
        pr.first = word;
        pr.second = num;
        // 放入容器
        _dict.push_back(pr);
    }

    ifstream ifs_index("data/word_index.dat");
    if (!ifs.is_open())
    {
        LogError("open dict.dat failed");
    }

    while (getline(ifs_index, line))
    { // 获得每一行
        // 处理非法单词
        string special_ch = ":{},";
        for (auto &ch : line)
        {
            if (special_ch.find(ch) != string::npos)
            {
                ch = ' ';
            }
        }
        // LogDebug("%s",line.c_str());
        istringstream iss(line);
        iss >> word; // 提取key
        while (iss >> num)
        { // 提取alue
            // 放入容器
            _index[word].insert(num);
        }
    }
    ifs.close();
    ifs_index.close();
}

vector<pair<string,int>> Dictionary::doQuery(const string &key)
{   
    vector<pair<string,int>> words;
    string word = key;
    // 将key切割成单个字母和汉字
    // 遍历单词的每个字母
    for (size_t j = 0; j < word.size();)
    { // 1000 0000 & ch
        if ((word[j] & 0x80) == 0)
        {
            //根据字母和汉字查找索引
            string ch = word.substr(j,1);
            for(auto & index :_index[ch]){
                words.push_back(_dict[index+1]);
                //LogDebug("%d",index);
            }
            
            ++j;
        }
        else if ((word[j] & 0xE0) == 0xC0)
        {
           //根据字母和汉字查找索引
            string ch = word.substr(j,2);
            for(auto & index :_index[ch]){
                words.push_back(_dict[index+1]);
                //LogDebug("%d",index);
            }
            j += 2;
        }
        else if ((word[j] & 0xF0) == 0xE0)
        {
            string ch = word.substr(j,3);
            for(auto & index :_index[ch]){
                words.push_back(_dict[index+1]);
                //LogDebug("%d",index);
            }
            j += 3;
        }
        else if ((word[j] & 0xF8) == 0xF0)
        {
            string ch = word.substr(j,4);
            for(auto & index :_index[ch]){
                words.push_back(_dict[index+1]);
                //LogDebug("%d",index);
            }
            j += 4;
        }else
        {
            ++j; // 避免死循环
        }
    }

    return words;
}

void Dictionary::print_dict()
{
    cout << _dict[0].first << " " << _dict[0].second << "\n";
    cout << _dict[1].first << " " << _dict[1].second << "\n";
    cout << _dict[32817].first << " " << _dict[32817].second << "\n";

    // for (auto &it : _index["("])
    // {
    //     LogDebug("%d", it);
    // }
    cout << "\n";
}



